from django.http import Http404
from django.shortcuts import render

# Create your views here.


from rest_framework.generics import ListAPIView, GenericAPIView
from rest_framework.response import Response
from rest_framework.views import APIView

from recruitments.models import City, Enterprise, Recruit
from recruitments.serializer import CitySerializer, RecuitsVisitSerializer, EnterpriseHotlistSerializer, \
    EnterpriseIDSerializer, RecruitsIdSerializer

# 7.1 热门城市
# GET /city/hotlist/
class CityViewSet(ListAPIView):
    queryset = City.objects.all()
    serializer_class = CitySerializer
    pagination_class = None


# 7.2 增加企业访问次数
#PUT /enterprise/{id}/visit/
# 好像可以写在序列化器里
class EnterpriseVisitViewSet(APIView):
    def put(self,request,pk):
        try:
            quertset = Enterprise.objects.get(id=pk)
        except:
            return Http404
        # 对相对应pk的浏览字段加1
        quertset.visits += 1
        quertset.save()
        return Response({
                         "message":"更新成功",
                         "success":"true"
                        })


# GET /recruits/search/recommend/
# GET /recruits/search/latest/
# 7.3 推荐职位
# 7.4 最新职位
class RecruitsRecommendViewSet(ListAPIView):
    queryset = Recruit.objects.all()
    serializer_class = RecuitsVisitSerializer
    pagination_class = None


# GET /enterprise/search/hotlist/
# 7.5 热门企业
class EnterpriseHotlist(ListAPIView):
    queryset = Enterprise.objects.all()
    serializer_class = EnterpriseHotlistSerializer
    pagination_class = None


# POST/enterprise/{id}/collect/
# 7.6 收藏公司
class EnterpriseCollect(APIView):
    def post(self,request,pk):
            # 取到此公司的对象
        collect = Enterprise.objects.get(id=pk)
        try:
            # 检查此公司收藏着里是否存在此ID存在删除
            collect.users.get(id=request.user.id)
            user = collect.users.remove(request.user.id)

        except:
            # 不存在添加
            user = collect.users.add(request.user.id)
        return Response({
            "message": "更新成功",
            "success": "true"
        })

# POST /recruits/search/city/keyword/
# 7.7 搜索职位
class RecruitsKeyword(GenericAPIView):
    def post(self,request):
        city = request.data['cityname']
        jobname = request.data['keyword']
        queryset = Recruit.objects.filter(city=city,jobname__contains=jobname)
        # , jobname__contains = jobname
        serializer = RecuitsVisitSerializer(queryset,many=True)
        return Response(serializer.data)



# GET /recruits/{id}/
# 7.8职位详情
class RecruitsIdViewSet(APIView):

    def get(self,reuqest,pk):
        queryset = Recruit.objects.get(id=pk)
        serializer = RecruitsIdSerializer(queryset)
        return Response(serializer.data)




# GET /recruits/{id}/visit/
# 7.9 增加职位访问次数
class RecruitsVisitViewSet(APIView):
    def put(self,request,pk):
        try:
            quertset = Recruit.objects.get(id=pk)
        except:
            return Http404
        # 对相对应pk的浏览字段加1
        quertset.visits += 1
        quertset.save()
        return Response({
                         "message":"更新成功",
                         "success":"true"
                        })



# POST /recruits/{id}/collect/
# 7.10 收藏职位
class RecruitsCollect(APIView):

    def post(self,request,pk):

        collect = Recruit.objects.get(id=pk)
        try:
            # 检查此公司收藏着里是否存在此ID存在删除
            collect.users.get(id=request.user.id)
            user = collect.users.remove(request.user.id)
        except:
            # 不存在添加
            user = collect.users.add(request.user.id)

        return Response({
            "message": "更新成功",
            "success": "true"
        })

 # GET /enterprise/{id}/
#  7.11 企业详情
class EnterpriseViewSet(APIView):

    def get(self,reuqest,pk):
        queryset = Enterprise.objects.get(id=pk)
        serializer = EnterpriseIDSerializer(queryset)
        return Response(serializer.data)